package com.modele.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.modele.dao.DAO;
import com.modele.entity.Consomptible;


public class ConsomptibleDAO extends DAO<Consomptible>{

	public ConsomptibleDAO(Connection connexion) {
		super(connexion);
	}

	@Override
	public boolean create(Consomptible obj) {
		try {
			PreparedStatement statement = connexion.prepareStatement("INSERT INTO consomptible " +
					"(nom, prix) " +
					"VALUES (?,?)");
			statement.setString(1, obj.getNom());
			statement.setFloat(2, obj.getPrix());

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	@Override
	public boolean delete(Consomptible obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Consomptible obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Consomptible find(int id) 
	{
		Consomptible consomptible = null;

		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM consomptible WHERE idConsomptible = ?");
			statement.setInt(1, id);

			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();
				if(result.next())
				{
					consomptible = new Consomptible();

					consomptible.setIdConsomptible(id);
					consomptible.setNom(result.getString(2));
					consomptible.setPrix(result.getFloat(3));						
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return consomptible;
	}
	@Override
	public List<Consomptible> findAll() 
	{
		List<Consomptible> listConsomptible = new ArrayList<Consomptible>();

		try 
		{
			PreparedStatement statement = connexion.prepareStatement("SELECT * FROM consomptible");

			if(statement.execute())
			{
				ResultSet result = statement.getResultSet();

				Consomptible consomptible = null;

				while(result.next())
				{

					consomptible = new Consomptible();

					int id = result.getInt(1);

					consomptible.setIdConsomptible(id);
					consomptible.setNom(result.getString(2));
					consomptible.setPrix(result.getFloat(3));	

					listConsomptible.add(consomptible);
				}
			}
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return listConsomptible;
	}

	@Override
	public Consomptible findByValue(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isPresent(Object obj) {
		// TODO Auto-generated method stub
		return false;
	}

}


